﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using DotNet.Configuration;
using DotNet.Mvc;
using XNWebAPI.Models;
using XNWebService;

namespace XNWebAPI.Controllers
{
    public class FlightController : JsonController
    {
        private List<StationModel> Stations;

        /// <summary>
        /// 航班时刻查询
        /// </summary>
        /// <param name="dep">出发机场代码，（如:BJS，机场代码，可在国际航班城市列表中查询）</param>
        /// <param name="arr">到达机场代码，（如:MYC）</param>
        /// <param name="date">查询日期（格式：yyyyMMdd, 如：20141118）</param>
        /// <param name="pageNum">查询页码，默认为1，每页返回20条记录</param>
        public async Task<ActionResult> FlightSearch(string dep, string arr, string date, int pageNum)
        {
            var result = await FlightService.FlightSearch(dep, arr, date, pageNum);
            return new JsonStringResult(result);
        }

        /// <summary>
        /// 航班机场查询
        /// </summary>
        public ActionResult FlightStations()
        {
            var result = System.IO.File.ReadAllText(Server.MapPath("~/FlightStations.json"));
            return new JsonStringResult(result);
        }

        /// <summary>
        /// 航班机场查询
        /// </summary>
        public ActionResult FlightStationsSearch(string term)
        {
            if (Stations == null)
            {
                Stations = JsonHelper.Deserialize(Server.MapPath("~/FlightStations.json"), new List<StationModel>());
            }
            //foreach (var item in Stations)
            //{
            //    item.spell = SpellHelper.ConvertSpell(item.name);
            //}
            //JsonHelper.Serialize(Server.MapPath("~/FlightStations.json"), Stations);
            var list = Stations.Where(p => p.name.StartsWith(term, StringComparison.OrdinalIgnoreCase)
            || p.spell.StartsWith(term, StringComparison.OrdinalIgnoreCase));
            return Json(list);
        }
    }
}